package com.notelapierdas.objects;

import android.os.Parcel;
import android.os.Parcelable;

public class Address implements Parcelable{
	
	private String name;
	private double latitude;
	private double longitude;
	
	public Address(String name, String coordinates){
		this.name= name;
		double[] coord = getCoordinates(coordinates);
		this.latitude = coord[0];
		this.longitude = coord[1];
	}


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	
	/////////////Parcelable//////////////////////////////
	
	public Address(Parcel in) {
		readFromParcel(in);
	}

	private void readFromParcel(Parcel in) {
		// TODO Auto-generated method stub
		name = in.readString();
		latitude = in.readDouble();
		longitude = in.readDouble();
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeString(name);
		dest.writeDouble(latitude);
		dest.writeDouble(longitude);
	}	

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@SuppressWarnings("rawtypes")
	public static final Parcelable.Creator CREATOR =
    	new Parcelable.Creator() {
            public Address createFromParcel(Parcel in) {
                return new Address(in);
            }
 
            public Address[] newArray(int size) {
                return new Address[size];
            }
        };
	
	///////////////////////////////////////////////////////
	
    	private double[] getCoordinates(String text) {
    		String splitter = ",";
    		if(text.contains(":")){
    			splitter=":";
    		}else if(text.contains(";")){
    			splitter=(";");
    		}else if(text.contains("-")){
    			splitter=("-");
    		}
    		String[] sa = text.split(splitter);
    		double[] da = new double[2];
    		da[0] = Double.valueOf(sa[0]);
    		da[1] = Double.valueOf(sa[1]);
    		    		
    		return da;
    	}
    	
    	public String getTextCoordinates(){
    		return (latitude+","+longitude);
    	}

}
